//app.js
App({
  onLaunch: function () {
    // 展示本地存储能力
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs);


    // 登录
    var js_code;
    wx.login({
      success: res => {
        console.log("=====================小程序登录==========================");
        console.log(res);
         js_code=res.code;
        wx.setStorageSync('js_code', js_code);
      }
    })

    // 获取用户信息
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
          wx.getUserInfo({
            success: res => {
              console.log("-----------------app.js getSetting----------------");
              console.log(res);
              // 可以将 res 发送给后台解码出 unionId
              this.globalData.userInfo = res.userInfo

              // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
              // 所以此处加入 callback 以防止这种情况
              if (this.userInfoReadyCallback) {
                this.userInfoReadyCallback(res)
              }
            }
          })
        }
      }
    })

    

  },

  //判断是否有网络
  networkType: function () {
    wx.getNetworkType({
      success(res) {
        const networkType = res.networkType;
        console.log(res);
        if (networkType == "none") {
          console.log("无网络");
          return 1;
        } else {
          console.log("获取网络情况");
          wx.navigateBack({
            delta:1
          })
          console.log(res.networkType);
          return 2;
        }
      }
    })
  },

  getOpenid: function () {
    var that = this;
    return new Promise(function (resolve, reject) {
      wx.login({
        success: function (res) {
          console.log(res)
          if (res.code) {
            //发起网络请求
            wx.request({
              url: 'https://www.jikecity.xin/small/login/get_openid',

              data: {
                js_code: res.code,
              },
              method: "POST",
              header: {
                "Content-Type": "application/x-www-form-urlencoded" //post
              },
              success(res) {
                console.log(res);
                that.globalData.openid = res.data.openid;
                wx.setStorageSync('openid', res.data.openid);//存储openid
                var res = {
                  status: 200,
                  data: res.data.openid
                }
                resolve(res);
              }
            })
          } else {
            console.log('登录失败！' + res.errMsg)
          }
        }
      });
    })
  }, 
  
  globalData: {
    userInfo: null,
    loadStatus:false,
    areaid: '', //pages/location/location 跳转 pages/index/index switchtab不能带参数 只能使用全局变量
    navId:'t',//首页选中的导航栏的id
    navId2: 't',//识堂选中的导航栏的id
    nav_index:0,//首页选中的频道的序号
    nav_index2: 0,//识堂选中的频道的序号
    navList:[],//用户选择的频道
    globalFormId:"",
    js_code: wx.getStorageSync("js_code")
  }
})